package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.AssistantBodySkin;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class AssistantBodySkinDaoImpl extends TempDataObjectDao<AssistantBodySkin> {
    public static String getAssistantBodySkinSql = "select * from `t_u_assistant_body_skin` where `userId`=?";

    public AssistantBodySkin getAssistantBodySkin(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAssistantBodySkinSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateAssistantBodySkinSql = "update `t_u_assistant_body_skin` set characterId=?,clothesId=?,pantsId=?,footId=?," +
            "headId=?,faceId=?,handId=?,otherId=?,desktopId=? where userId=?";

    @Override
    protected boolean updateData(AssistantBodySkin assistantBodySkin) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getCharacterId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getClothesId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getPantsId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getFootId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getHeadId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getFaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getHandId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getOtherId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getDesktopId()));
        para.put(i++, new DbParameter(Types.BIGINT, assistantBodySkin.getUserId()));
        if (getDb().executeNoneQuery(updateAssistantBodySkinSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAssistantBodySkinSql = "insert into `t_u_assistant_body_skin`(userId,characterId,clothesId,pantsId,footId," +
            "headId,faceId,handId,otherId,desktopId) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(AssistantBodySkin assistantBodySkin) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, assistantBodySkin.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getCharacterId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getClothesId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getPantsId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getFootId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getHeadId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getFaceId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getHandId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getOtherId()));
        para.put(i++, new DbParameter(Types.INTEGER, assistantBodySkin.getDesktopId()));
        if (getDb().executeNoneQuery(insertAssistantBodySkinSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected AssistantBodySkin parseData(ResultSet rs) throws SQLException {
        AssistantBodySkin assistantBodySkin = new AssistantBodySkin();
        assistantBodySkin.setUserId(rs.getLong("userId"));
        assistantBodySkin.setCharacterId(rs.getInt("characterId"));
        assistantBodySkin.setClothesId(rs.getInt("clothesId"));
        assistantBodySkin.setPantsId(rs.getInt("pantsId"));
        assistantBodySkin.setFootId(rs.getInt("footId"));
        assistantBodySkin.setHeadId(rs.getInt("headId"));
        assistantBodySkin.setFaceId(rs.getInt("faceId"));
        assistantBodySkin.setHandId(rs.getInt("handId"));
        assistantBodySkin.setOtherId(rs.getInt("otherId"));
        assistantBodySkin.setDesktopId(rs.getInt("desktopId"));

        return assistantBodySkin;
    }

}